﻿using DCIMAP.GANS.GeneticAlgorithms;
using DCIMAP.GANS.GeneticAlgorithms.Kit;
using System.Collections;
using GAIQ;
using System;

using DCIMAP.GANS.GeneticAlgorithms.Legacy;

[System.Serializable]
public class IQCrosser : Crosser
{
    static Random _r = new Random();
    
    public override Gene Cross(Gene mother, Gene father)
    {

        StringArrayGene recipient = (StringArrayGene)mother;
        StringArrayGene donor = (StringArrayGene)father;
        int to = Algorithm.Random.Next(recipient.StringCode.Length); //возможно все, за исключением корня
        int from = Algorithm.Random.Next(donor.StringCode.Length);
        int[] subtree1 = recipient.getSubtree(to);
        int[] subtree2 = donor.getSubtree(from);
        StringArrayGene son = new StringArrayGene(subtree1[0] + (subtree2[1] - subtree2[0] + 1) + (recipient.StringCode.Length - subtree1[1] - 1));
        int sonIx = 0;
        for(int i =0;i<subtree1[0];++i)
        {
            son.StringCode[sonIx] = recipient.StringCode[i];
            ++sonIx;
        }
        for(int i=subtree2[0];i<=subtree2[1];++i)
        {
            son.StringCode[sonIx] = donor.StringCode[i];
            ++sonIx;
        }
        for(int i=subtree1[1]+1;i<recipient.StringCode.Length;++i)
        {
            son.StringCode[sonIx] = recipient.StringCode[i];
            ++sonIx;
        }
        
        return son;
    }
}
